#import "AllinSHAUtil.h"
#import <CommonCrypto/CommonDigest.h>
@implementation AllinSHAUtil
//SHA
/*
 HASH算法     16进制序列位数      字节数       比特数
 CC_SHA1        40              20          160
 CC_SHA256      64              32          256
 CC_SHA384      96              48          384
 CC_SHA512      128             64          512
 */
+ (NSString *) encryptSHA1 : (NSString *) str {
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [AllinSHAUtil encryptSHA1Data:data];
}
+ (NSString *) encryptSHA1Data : (NSData *) data {
    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
    (void) CC_SHA1( [data bytes], (CC_LONG)[data length], digest );
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}

+ (NSString *) encryptSHA256 : (NSString *) str {
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [AllinSHAUtil encryptSHA256Data:data];
}

+ (NSString *) encryptSHA256Data : (NSData *) data {
    unsigned char digest[CC_SHA256_DIGEST_LENGTH];
    (void) CC_SHA256( [data bytes], (CC_LONG)[data length], digest );
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}

+ (NSString *) encryptSHA384 : (NSString *) str {
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [AllinSHAUtil encryptSHA384Data:data];
}


+ (NSString *) encryptSHA384Data : (NSData *) data {
    unsigned char digest[CC_SHA384_DIGEST_LENGTH];
    (void) CC_SHA384( [data bytes], (CC_LONG)[data length], digest );
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA384_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA384_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}
+ (NSString *) encryptSHA512 : (NSString *) str {
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    return [AllinSHAUtil encryptSHA512Data:data];;
}
+ (NSString *) encryptSHA512Data : (NSData *) data {
    unsigned char digest[CC_SHA512_DIGEST_LENGTH];
    (void) CC_SHA512( [data bytes], (CC_LONG)[data length], digest );
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return output;
}


@end
